home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / benchmarks / read / read.c.old < prev    next >
Encoding:
Text File  |  1989-09-01  |  3.5 KB  |  162 lines

  1. /*
  2.  * read.c --
  3.  *    Benchmark for the read system call.
  4.  *    This can also do stress testing by passing in bogus arguments.
  5.  */
  6. #include "sprite.h"
  7. #include "sys/time.h"
  8. #include "sys/file.h"
  9. #include "fs.h"
  10. #include "sig.h"
  11. #include "stdio.h"
  12. #include "option.h"
  13.  
  14. static char *buffer;
  15.  
  16. int    repeats = 1;
  17. int    blockSize = 16384;
  18. Boolean errorTest = FALSE;
  19.  
  20. Option optionArray[] = {
  21.     {OPT_INT, "r", (Address) &repeats,
  22.      "\tNumber of times to repeat read (Default 1)."},
  23.     {OPT_INT, "b", (Address) &blockSize, 
  24.      "\tBlock size to use for reading (Default 16384)."},
  25.     {OPT_TRUE, "e", (Address)&errorTest,
  26.      "\tTest error cases. "},
  27. };
  28. int numOptions = sizeof(optionArray) / sizeof(Option);
  29.  
  30. int Handler();
  31. int gotSig = FALSE;
  32.  
  33. main(argc, argv)
  34. int argc;
  35. char **argv;
  36. {
  37.     int         cnt, total;
  38.     double         rate, tmp;
  39.     struct timeval    before, after;
  40.     int            newOffset;
  41.     ReturnStatus    status;
  42.     Sig_Action        newAction, oldAction;
  43.     register    int    i;
  44.  
  45.     argc = Opt_Parse(argc, argv, optionArray, numOptions, 0);
  46.  
  47.     /*
  48.      * Set up signal handling, trap interrupts in order to test
  49.      * the GEN_INTERRUPTED_BY_SIGNAL return code.
  50.      */
  51.     newAction.action = SIG_HANDLE_ACTION;
  52.     newAction.handler = Handler;
  53.     newAction.sigHoldMask = 0;
  54.     Sig_SetAction(SIG_INTERRUPT, &newAction, &oldAction);
  55.  
  56.     buffer = (char *)malloc(blockSize);
  57.  
  58.     if (errorTest) {
  59.     int numErrors = 0;
  60.     printf("Read Error Tests\n"); fflush();
  61.  
  62.     cnt = write(1, "? ", sizeof("? "));
  63.     cnt = read(-2, 0, 0);
  64.     if (cnt >= 0) {
  65.         printf("ERROR: read(fd == -2) worked!\n");
  66.         numErrors++;
  67.     } else {
  68.         perror("read(fd == -2)");
  69.     }
  70.  
  71.     cnt = write(1, "? ", sizeof("? "));
  72.     cnt = read(0, -1, 10);
  73.     if (cnt >= 0) {
  74.         printf("ERROR: read{buffer = -1} worked!\n");
  75.         numErrors++;
  76.     } else {
  77.         perror(status, "read{buffer = -1}");
  78.     }
  79.  
  80.     write(1, "? ", 3);
  81.     cnt = read(0, buffer, -1);
  82.     if (cnt >= 0) {
  83.         printf("ERROR: read{count < 0} worked!\n");
  84.         numErrors++;
  85.     } else {
  86.         perror(status, "read{count < 0}");
  87.     }
  88.  
  89.     {
  90.         int outFD2;
  91.         outFD2 = open("/dev/null", O_WRONLY, 0);
  92.         if (outFD2 < 0) {
  93.         perror("Can't open /dev/null for writing");
  94.         } else {
  95.         cnt = read(outFD2, buffer, 10);
  96.         if (cnt >= 0) {
  97.             printf("ERROR: read{writeonly stream} worked!\n");
  98.             numErrors++;
  99.         } else {
  100.             perror(status, "read{writeonly stream}");
  101.         }
  102.         }
  103.     }
  104.  
  105.     {
  106.         char *newBuf = (char *)malloc(100 * 1024);
  107.         printf("Starting 100K read... "); fflush();
  108.         status = Fs_RawRead(0, 100 * 1024, newBuf, &cnt);
  109.         if (gotSig) {
  110.         printf("Got Signal, "); fflush();
  111.         }
  112.         if (status == SUCCESS) {
  113.         printf("Read %d bytes\n", cnt);
  114.         } else {
  115.         Stat_PrintMsg(status, "read");
  116.         }
  117.     }
  118.  
  119.     close(0);
  120.     write(1, "? ", 3);
  121.     cnt = read(0, buffer, 10);
  122.     if (cnt >= 0) {
  123.         printf("ERROR: read{closed stream} worked!\n");
  124.         numErrors++;
  125.     } else {
  126.         perror("read{closed stream}");
  127.     }
  128.     if (numErrors) {
  129.         printf("Read Error Test had %d errors\n", numErrors);
  130.     } else {
  131.         printf("No errors\n");
  132.     }
  133.     exit(numErrors);
  134.     } else {
  135.     total = 0;
  136.     gettimeofday(&before, NULL);
  137.     for ( ; repeats > 0; repeats--) {
  138.         Ioc_Reposition(0, IOC_BASE_ZERO, 0, &newOffset);
  139.         while (1) {
  140.         cnt = read(0, buffer, blockSize);
  141.         total += cnt;
  142.         if (cnt < blockSize) break;
  143.         }
  144.     }
  145.     gettimeofday(&after, NULL);
  146.     rate = after.tv_sec - before.tv_sec;
  147.     rate += (after.tv_usec - before.tv_usec)*.000001;
  148.     if (total <= 0) {
  149.         printf("%d bytes read in sec.\n", total, rate);
  150.     } else {
  151.         rate = total/rate;
  152.         printf("%d bytes read at %.0f bytes/sec.\n", total, rate);
  153.     }
  154.     }
  155. }
  156.  
  157. int
  158. Handler()
  159. {
  160.     gotSig = TRUE;
  161. }
  162.